package cn.wuxing.mediation.web.controller.system;

import cn.wuxing.mediation.common.core.controller.BaseController;
import cn.wuxing.mediation.common.core.domain.AjaxResult;
import cn.wuxing.mediation.common.utils.poi.ExcelUtil;
import cn.wuxing.mediation.system.domain.vo.DisputeDeleteQueryVo;
import cn.wuxing.mediation.system.domain.vo.DisputeOrdVo;
import cn.wuxing.mediation.system.domain.vo.DisputePreVo;
import cn.wuxing.mediation.system.domain.vo.ReceptionAreaVo;
import cn.wuxing.mediation.system.service.ISIndexService;
import cn.wuxing.mediation.system.service.ScreenService;
import com.alibaba.fastjson.JSON;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/mediation/screen")
public class ScreenController extends BaseController {

    @Resource
    private ScreenService screenService;

    @Resource
    private ISIndexService indexService;

    /**
     * 接待地图情况区级
     *
     * @return
     */
    @GetMapping("/receptionTown")
    public AjaxResult getReceptionTown() {
        List<ReceptionAreaVo> receptionArea = screenService.getReceptionAreaTown();
        return AjaxResult.success(receptionArea);
    }

    /**
     * 乡
     * @return
     */
    @GetMapping("/receptionVillage")
    public AjaxResult getReceptionVillage() {
        List<ReceptionAreaVo> receptionArea = screenService.getReceptionAreaVillage();
        return AjaxResult.success(receptionArea);
    }

    /**
     * 网格
     * @return
     */
    @GetMapping("/receptionGrid")
    public AjaxResult getReceptionGrid() {
        List<ReceptionAreaVo> receptionArea = screenService.getReceptionAreaGrid();
        return AjaxResult.success(receptionArea);
    }

    /**
     * 纠纷调解总况
     *
     * @param dateType
     * @return
     */
    @GetMapping("/disputeOverall")
    public AjaxResult getDisputeOverall(@RequestParam(value = "dateType", defaultValue = "DAY") String dateType) {
        Map<String, Object> disputeOverall = screenService.getDisputeOverall(dateType);
        return AjaxResult.success(disputeOverall);
    }

    /**
     * 纠纷主体类型统计
     *
     * @return
     */
    @GetMapping("/disputeBodyType")
    public AjaxResult getDisputeBodyType() {
        Map<String, Object> disputeOverall = screenService.getDisputeBodyTypeAll();
        return AjaxResult.success(disputeOverall);
    }

    /**
     * 纠纷类型占比
     *
     * @return
     */
    @GetMapping("/proportionTypes")
    public AjaxResult ProportionOfDisputeTypes() {
        List<Map<String, Object>> disputeOverall = screenService.proportionOfDisputeTypes();
        return AjaxResult.success(disputeOverall);
    }

    /**
     * 专窗办理统计
     *
     * @return
     */
    @GetMapping("/windowMediationSuccess")
    public AjaxResult windowMediationSuccess() {
        List<Map<String, Object>> disputeOverall = screenService.windowMediationSuccess();
        return AjaxResult.success(disputeOverall);
    }

    /**
     * 优秀调解员
     */
    @GetMapping("/excellentMediator")
    public AjaxResult excellentMediator() {
        List<Map<String, Object>> excellentMediator = screenService.excellentMediator();
        return AjaxResult.success(excellentMediator);
    }

    /**
     * 纠纷调解进度实况
     */
    @GetMapping("/progressFacts")
    public AjaxResult progressFacts() {
        List<Map<String, Object>> progressFacts = screenService.progressFacts();
        return AjaxResult.success(progressFacts);
    }

    /**
     * 纠纷调解进度实况详情
     */
    @GetMapping("/progressFactsDetail")
    public AjaxResult progressFactsDetail(@RequestParam("eventId") Long eventId,
                                           @RequestParam("type") String type) {
        Map<String, Object> progressFacts = screenService.progressFactsDetail(eventId, type);
        return AjaxResult.success(progressFacts);
    }

    /**
     * 矛盾来源
     */
    @GetMapping("/disputeSource")
    public AjaxResult disputeSource() {
        List<Map<String, Object>> progressFacts = screenService.disputeSource();
        return AjaxResult.success(progressFacts);
    }

    /**
     * 已删除矛盾查询
     *
     */
    @PostMapping("/disputeDelete")
    public AjaxResult disputeDeleteList(@RequestBody DisputeDeleteQueryVo disputeDeleteQueryVo) {
        List<Object> data = indexService.disputeDeleteList(disputeDeleteQueryVo);
        return AjaxResult.success(data);
    }

    /**
     * 已删除矛盾还原
     *
     */
    @PostMapping("/disputeRestore")
    public AjaxResult disputeRestore(@RequestBody DisputeDeleteQueryVo disputeDeleteQueryVo) {
        int data = indexService.disputeRestore(disputeDeleteQueryVo);
        return AjaxResult.success(data);
    }


    /**
     * 导出
     * @param response
     * @param disputeDeleteQueryVo
     */
    @PostMapping("/disputeDeleteExport")
    public void export(HttpServletResponse response, DisputeDeleteQueryVo disputeDeleteQueryVo) {

        List<Object> data = indexService.disputeDeleteList(disputeDeleteQueryVo);
        ExcelUtil<DisputeOrdVo> ordVoExcelUtil = new ExcelUtil<DisputeOrdVo>(DisputeOrdVo.class);
        ExcelUtil<DisputePreVo> preVoExcelUtil = new ExcelUtil<DisputePreVo>(DisputePreVo.class);
        List<DisputeOrdVo> disputeOrdVos=new ArrayList<>();
        List<DisputePreVo> disputePreVos=new ArrayList<>();
        if(disputeDeleteQueryVo.getCategory().equals("2")){
            data.forEach(i->{
                DisputeOrdVo disputeOrdVo = JSON.parseObject(JSON.toJSONString(i), DisputeOrdVo.class);
                disputeOrdVos.add(disputeOrdVo);
            });
            ordVoExcelUtil.exportExcel(response, disputeOrdVos, "已删除一般纠纷件");
        }else {
            data.forEach(i->{
                DisputePreVo disputePreVo = JSON.parseObject(JSON.toJSONString(i), DisputePreVo.class);
                disputePreVos.add(disputePreVo);
            });
            System.out.println(disputePreVos);
            preVoExcelUtil.exportExcel(response, disputePreVos, "已删除诉前调解件");
        }
    }
}
